/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import com.michaelbaranov.microba.calendar.DatePicker;
import domain.Task;
import service.TaskManager;

import javax.swing.*;
import java.beans.PropertyVetoException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Дмитрий
 */
public class ChangeFrame extends JDialog {

    private TaskManager manager;
    private Task task;
    private DateFormat f = new SimpleDateFormat("dd/MM/yyyy-HH:mm");
    private boolean isUpdated = false;
    private TaskTableModel allTableModel;
    private TaskTableModel activeTableModel;

    /**
     * Creates new form ChangeFrame
     */
    public ChangeFrame(TaskManager manager, Task task, TaskTableModel model, TaskTableModel activeModel) throws PropertyVetoException {
        super();
        this.manager = manager;
        this.task = task;
        this.activeTableModel = activeModel;
        initComponents();
        if (task.getName() != null) {
            NameText.setText(task.getName());
            datePicker.setDate(task.getDate());
            DescrTextArea.setText(task.getDescription());
            isUpdated = true;
        }
        this.allTableModel = model;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NameLabel = new javax.swing.JLabel();
        DateLabel = new javax.swing.JLabel();
        DescrLabel = new javax.swing.JLabel();
        NameText = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        ChangeLabel = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        DatePickerPane = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        DescrTextArea = new javax.swing.JTextArea();

        setTitle("Changing Task");

        NameLabel.setText("Name");

        DateLabel.setText("Date");

        DescrLabel.setText("Description");

        NameText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NameTextActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        ChangeLabel.setText("Enter required parametres");

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        datePicker = new DatePicker();
        datePicker.setKeepTime(true);
        datePicker.setStripTime(false);
        datePicker.setDateFormat(new SimpleDateFormat("dd/MM/yyyy HH:mm"));

        javax.swing.GroupLayout DatePickerPaneLayout = new javax.swing.GroupLayout(DatePickerPane);
        DatePickerPane.setLayout(DatePickerPaneLayout);
        DatePickerPaneLayout.setHorizontalGroup(
                DatePickerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(datePicker, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
        );
        DatePickerPaneLayout.setVerticalGroup(
                DatePickerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(datePicker)
        );

        jScrollPane1.setViewportView(jTextPane1);

        DescrTextArea.setColumns(20);
        DescrTextArea.setRows(5);
        jScrollPane2.setViewportView(DescrTextArea);
        jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        //jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        DatePickerPane.add(new DatePicker());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(85, 85, 85)
                                                .addComponent(jButton1)
                                                .addGap(18, 18, 18)
                                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(90, 90, 90)
                                                .addComponent(ChangeLabel))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(34, 34, 34)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(DateLabel)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                .addComponent(DatePickerPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(DescrLabel)
                                                                        .addComponent(NameLabel))
                                                                .addGap(21, 21, 21)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(NameText, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                                .addContainerGap(40, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addContainerGap(180, Short.MAX_VALUE)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(118, 118, 118)))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(ChangeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(NameLabel)
                                        .addComponent(NameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(DatePickerPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(DateLabel))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(11, 11, 11)
                                                .addComponent(DescrLabel)
                                                .addContainerGap())
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jButton2)
                                                        .addComponent(jButton1))
                                                .addGap(0, 7, Short.MAX_VALUE))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addContainerGap(111, Short.MAX_VALUE)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap(112, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void NameTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NameTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_NameTextActionPerformed

    private void DateTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DateTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DateTextActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        String name;
        Date date;
        String description;

        try {

            name = NameText.getText();
            date = datePicker.getDate();
            description = DescrTextArea.getText();
            if ("".equals(name) || date == null || "".equals(description)) {
                showWarningMessage("Warning", "Please, enter necessary parameters of the task");
                return;
            }
            if ((new Date()).compareTo(date) >= 0) {
                showWarningMessage("Warning", "Due date should be greater then current date");
                return;
            }
            if (isUpdated) {
                if (manager.checkVersion(task.getId(), task.getVersion())) {
                    //Если задача уже показана во вкладке active удаляем ее
                    activeTableModel.getTasks().remove(task);
                    activeTableModel.repaint();

                    task = manager.update(task.getId(), name, description, date, task.getVersion());
                } else {
                    showWarningMessage("Task has been change", "Please, click refresh button to update list of tasks");
                }
            } else {
                task = manager.add(name, description, date);
            }
            allTableModel.repaint();
            this.dispose();
        } catch (Exception e) {
            e.printStackTrace();
            showWarningMessage("Something goes wrong...", "Please, check parameters of the task");
        }
    }


    private static void showWarningMessage(String title, String msg) {
        JOptionPane.showMessageDialog(null, msg, title, JOptionPane.WARNING_MESSAGE);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private DatePicker datePicker;
    private javax.swing.JLabel ChangeLabel;
    private javax.swing.JLabel DateLabel;
    private javax.swing.JPanel DatePickerPane;
    private javax.swing.JLabel DescrLabel;
    private javax.swing.JTextArea DescrTextArea;
    private javax.swing.JLabel NameLabel;
    private javax.swing.JTextField NameText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
}
